const { Controller } = require("egg");
const Message = require('../utils/message')

class StudentController extends Controller {
  async findAll () {
    const { ctx, service } = this
    // 1.获取参数(参数校验)
    // ctx.query
    // ctx.request.body
    // 2.调用service拿到数据库查询数据
    let students = await service.student.findAll()

    // 3.做出响应
    ctx.body = new Message(200, '查询成功', students)
  }

  async saveOrUpdate () {
    const { ctx, service } = this
    // 1.接收参数
    let stu = ctx.request.body
    // 2.调用service
    await service.student.saveOrUpdate(stu)
    // 3.做出响应
    ctx.body = new Message(200, '操作成功')
  }

  async deleteById () {
    const { ctx, service } = this
    await service.student.deleteById(ctx.query.id)
    ctx.body = new Message(200, '删除成功')
  }

  async pageQuery () {
    const { ctx, service } = this
    let students = await service.student.pageQuery(ctx.query)
    ctx.body = new Message(200, '查询成功', students)
  }

}

module.exports = StudentController